#include "server.hpp"
#include <iostream>
#include <sys/socket.h>   // send, MSG_NOSIGNAL
#include <unistd.h>       // close, read, write …

int main() {
    try {
        uds_model::Server srv("/tmp/ck_example.sock");
        /* 自定义业务：把收到的行打印出来，再 echo 回去 */
        srv.set_on_line([](int fd, std::string ln){
            std::cout << "[fd=" << fd << "] " << ln << std::endl;
            ln += '\n';
            send(fd, ln.data(), ln.size(), MSG_NOSIGNAL);
        });
         // 设置连接/断开回调
        srv.set_on_connected([](int fd) {
            std::cout << "Client connected: " << fd << std::endl;
        });
        
        srv.set_on_disconnected([](int fd) {
            std::cout << "Client disconnected: " << fd << std::endl;
        });
        srv.run();          // 阻塞直到出错或信号
    } catch (const std::system_error& e) {
        std::cerr << "server start failed: " << e.what() << '\n';
        return 1;   // systemd/supervisor 看到非 0 即可报警或重启
    }
}
